package com.hb.trade.server.project.complaint.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.complaint.dto.ComplaintDTO;
import com.hb.trade.server.project.complaint.service.ComplaintService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 投诉异议 控制器
 *
 * @author ljs
 * @date 2022-1-26 08:45:44
 */
@RestController
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;

    /**
     * 保存投标异议
     * @param complaintDTO
     * @return
     */
    @PostMapping("/complaint/save")
    public Message saveComplaint(@RequestBody ComplaintDTO complaintDTO) {
        if (null == complaintDTO || complaintDTO.getSectionId() == null) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        // 如果有ID则直接改为已提交状态
        if(complaintDTO.getId()!=null){
           complaintDTO.setComplaintStatus(2);
           boolean result =  complaintService.updateById(complaintDTO);
           return result? MsgUtil.success(complaintDTO.getId()):MsgUtil.fail(MsgEnum.FAILURE.msg());
        }
        complaintDTO.buildBaseData(1);
        // 默认状态为线上投诉
        complaintDTO.setComplaintStatus(1);
        // 默认状态为保存
        if (null == complaintDTO.getComplaintStatus()) {
            complaintDTO.setComplaintWay(1);
        } else {
            complaintDTO.setComplaintWay(complaintDTO.getComplaintStatus());
        }
        boolean saveResult = complaintService.save(complaintDTO);
        if (saveResult) {
            return MsgUtil.success(complaintDTO.getId());
        } else {
            return MsgUtil.fail(MsgEnum.FAILURE.msg());
        }
    }

    @PostMapping("/complaint/commitComplaint")
    public Message commitComplaint(@RequestBody ComplaintDTO complaintDTO) {
        if (null == complaintDTO.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        // 修改状态为已提交
        complaintDTO.setComplaintStatus(2);
        complaintDTO.buildBaseData(2);
        boolean updateResult = complaintService.updateById(complaintDTO);
        return updateResult ? MsgUtil.msg(MsgEnum.SUCCESS) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    @PostMapping("/complaint/finishComplaint")
    public Message finishComplaint(@RequestBody ComplaintDTO complaintDTO) {
        if (null == complaintDTO.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        // 修改状态为结束投诉
        complaintDTO.setComplaintStatus(3);
        complaintDTO.buildBaseData(2);
        boolean updateResult = complaintService.updateById(complaintDTO);
        return updateResult ? MsgUtil.msg(MsgEnum.SUCCESS) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    /**
     * 招标人查询投诉异议接口
     *
     * @param complaintDTO
     * @return
     */
    @PostMapping("/complaint/pageComplaint")
    public Message<PageResult<ComplaintDTO>> pageComplaint(@RequestBody ComplaintDTO complaintDTO) {
        if (null == complaintDTO || null == complaintDTO.getSubmitOrgId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        Page<ComplaintDTO> complaintDTOPage = complaintService.pageComplaint(complaintDTO);
        PageResult<ComplaintDTO> complaintDTOPageResult = PageResult.get(complaintDTOPage);
        return MsgUtil.success(complaintDTOPageResult);
    }

    /**
     * 投标人查询投诉异议接口
     *
     * @param complaintDTO
     * @return
     */
    @PostMapping("/complaint/pageTenderComplaint")
    public Message<PageResult<ComplaintDTO>> pageTenderComplaint(@RequestBody ComplaintDTO complaintDTO) {
        if (null == complaintDTO || null == complaintDTO.getSubmitOrgId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
//        LambdaQueryWrapper<Complaint> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Complaint::getSubmitOrgId, complaintDTO.getSubmitOrgId());
//        if (null != complaintDTO.getProjectName()) {
//            lambdaQueryWrapper.like(Complaint::getProjectName, complaintDTO.getProjectName());
//        }
        Page<ComplaintDTO> complaintPage = complaintService.pageTenderComplaint(complaintDTO);
        PageResult<ComplaintDTO> complaintPageResult = PageResult.get(complaintPage);
        return MsgUtil.success(complaintPageResult);
    }

    /**
     * 获取投诉异议详情信息（包括投诉和回复）
     *
     * @param complaintDTO
     * @return
     */
    @PostMapping("/complaint/complaintDetail")
    public Message<ComplaintDTO> complaintDetail(@RequestBody ComplaintDTO complaintDTO) {
        if (null == complaintDTO || null == complaintDTO.getId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        ComplaintDTO complaintResultDTO = complaintService.complaintDetail(complaintDTO);
        return MsgUtil.success(complaintResultDTO);
    }
}
